package com.rs.game.player.content;

import java.util.HashMap;
import java.util.Map;

import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.Animation;
import com.rs.game.item.Item;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;

public class Burying {
	
public enum Bone {
	NORMAL(526, 4.5),
	BURNT(528, 4.5), 
	WOLF(2859, 4.5), 
	MONKEY(3183, 4.5), 
	BAT(530, 5.2), 
	BIG(532, 15), 
	JOGRE(3125, 15), 
	ZOGRE(4812, 22.5), 
	SHAIKAHAN(3123, 25),
	BABY(534, 30), 
	WYVERN(6812, 50), 
	DRAGON(536, 72), 
	FAYRG(4830, 84), 
	RAURG(4832, 96), 
	DAGANNOTH(6729, 125), 
	OURG(4834, 140), 
	FROST_DRAGON(18830, 180),
	IMPIOUS_ASHES(20264, 4),
	ACCURSED_ASHES(20266, 12.5),
	INFERNAL_ASHES(20268, 62.5);

	private int id;
	private double experience;

	private static Map<Integer, Bone> bones = new HashMap<Integer, Bone>();

	static {
		for (Bone bone : Bone.values()) {
			bones.put(bone.getId(), bone);
		}
	}

	public static Bone forId(int id) {
		return bones.get(id);
	}

	private Bone(int id, double experience) {
		this.id = id;
		this.experience = experience;
	}

	public int getId() {
		return id;
	}

	public double getExperience() {
		return experience;
	}

	public static final Animation BURY_ANIMATION = new Animation(827);

	public static void bury(final Player player, int inventorySlot) {
		final Item item = player.getInventory().getItem(inventorySlot);
		if (item == null || Bone.forId(item.getId()) == null)
			return;
		if (player.getBoneDelay() > Utils.currentTimeMillis())
			return;
		final Bone bone = Bone.forId(item.getId());
		final ItemDefinitions itemDef = new ItemDefinitions(item.getId());
		player.addBoneDelay(3000);
		player.getPackets().sendSound(2738, 0, 1);
		player.setNextAnimation(BURY_ANIMATION);
		player.getPackets().sendGameMessage("You dig a hole in the ground...");
		WorldTasksManager.schedule(new WorldTask() {
			@Override
			public void run() {
				handleNecklaces(player, bone.getId());

				player.getPackets().sendGameMessage("You bury the " + itemDef.getName().toLowerCase());
				player.getInventory().deleteItem(item.getId(), 1);
				double xp = bone.getExperience() * player.getAuraManager().getPrayerMultiplier();
				player.getSkills().addXp(Skills.PRAYER, xp);
				player.addBoneDelay(0);
				Double lastPrayer = (Double) player.getTemporaryAttributes().get("current_prayer_xp");
				if (lastPrayer == null) {
					lastPrayer = 0.0;
				}
				double total = xp + lastPrayer;
				int amount = (int) (total / 500);
				if (amount != 0) {
					double restore = player.getAuraManager().getPrayerRestoration() * (player.getSkills().getLevelForXp(Skills.PRAYER) * 10);
					player.getPrayer().restorePrayer((int) (amount * restore));
					total -= amount * 500;
				}
				player.getTemporaryAttributes().put("current_prayer_xp", total);
				stop();
			}

		}, 2);
	}
}

public static void handleNecklaces(Player player, int itemId) {
	ItemDefinitions itemDef = ItemDefinitions.getItemDefinitions(itemId);
	int prayerGain = 0;
	if (itemDef.getName().toLowerCase().contains("dragon") || itemDef.getName().toLowerCase().contains("ourg")) {
		switch (player.getEquipment().getAmuletId()) {
		case 19888:
			prayerGain = 300;
			break;
		case 19887:
		case 19886:
			prayerGain = 10;
			break;
		default:
			break;
		}
	} else if (itemDef.getName().toLowerCase().contains("big")) {
		switch (player.getEquipment().getAmuletId()) {
		case 19888:
		case 19887:
			prayerGain = 200;
			break;
		case 19886:
			prayerGain = 10;
			break;
		default:
			break;
		}
	} else if (itemDef.getName().toLowerCase().equals("bones")) {
		switch (player.getEquipment().getAmuletId()) {
		case 19888:
		case 19887:
		case 19886:
			prayerGain = 100;
			break;
		default:
			break;
		}
	}
	player.getPrayer().restorePrayer(prayerGain);
}
}
